package com.api.gbhg.zengping.utils;

import com.api.gbhg.liuwenbo.dao.ProjectDetailsUtil;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.entity.Info;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.soa.workflow.request.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @description: this is a class
 * @author: Mr.zeng
 * @create: 2021-02-03 11:15
 **/
public class CodeCreateWorkFlow {

    /**
     * 得到所有预算来源信息
     * @return
     */
    public Map getYslyInfo(){
        Map<String,Integer> map=new HashMap<>();
        RecordSet rs=new RecordSet();
        String sql="select xh,yslymc from uf_budget_sources";
        rs.executeQuery(sql);
        while (rs.next()){
            int xh = rs.getInt("xh");
            String yslymc = rs.getString("yslymc");
            map.put(yslymc,xh);
        }
        return map;
    }
    public static final Logger LOGGER= LoggerFactory.getLogger(CodeCreateWorkFlow.class);
    /**
     * 创建工作流
     * @param info
     * @return
     */
        public String createWorkFlow(Info info) {
            RequestInfo requestinfo = new RequestInfo();
            //创建者ID
            requestinfo.setCreatorid("1");
            // 工作流ID
            requestinfo.setWorkflowid("91");
            // 紧急程度:0正常，1重要，2紧急
            requestinfo.setRequestlevel("0");
            // 提醒类型
            requestinfo.setRemindtype("1");
            //不自动流转至下一节点
            requestinfo.setIsNextFlow("0");
            requestinfo.setDescription("標題");
            MainTableInfo table = new MainTableInfo();
            // 主表
            Map<String, Object> mainTable = info.getTable();
            // 循环添加对应的主表字段
            for (String key:mainTable.keySet()
            ) {
                if("requestId".equals(key)){
                    // 流程id
                    requestinfo.setWorkflowid(ProjectDetailsUtil.getFlowIdByRequestId(mainTable.get(key).toString()));
                    continue;
                }
                // key 为id的 或者 key 为"" 都continue 继续下一层循环
                if("id".equals(key)||mainTable.get(key).equals("")){
                    continue;
                }
                String value = (String) mainTable.get(key);
                if("title".equals(key)){
                    requestinfo.setDescription(value);
                }
                table.addProperty(getPro(key,value));
            }
            requestinfo.setMainTableInfo(table);

            //明細表
            DetailTableInfo tables = new DetailTableInfo();
            // 明細表1
            if(null!=info.getData()){
                //表示明细表1
                DetailTable oneTable = new DetailTable();
                oneTable.setId("1");
                Map<String, Object>[] detailTable = info.getData();
                // 明细表行数
                for (int i = 0; i <detailTable.length; i++) {
                    Row row = new Row();
//                    Cell czlx=null;
                    for (String key:detailTable[i].keySet() ) {
//                        czlx = ;
                        if(key.equals("lx")){
                            Integer selectionBox = CommonDao.getSelectionBox(key, String.valueOf(detailTable[i].get(key)), null);
                            detailTable[i].put(key,selectionBox);
                        }
                        //过滤掉
                        if(key.equals("id")||key.equals("mainid")){
                            continue;
                        }
                        String value = String.valueOf(detailTable[i].get(key));
                        if(key.equals("bmyszcjjfl")){
                            value=value.substring(value.indexOf("[")+1,value.indexOf("]"));
                        }
                        //操作类型
                        row.addCell(getCell(key,value));
                    }
                    LOGGER.info("row1-->"+row.enumerateCell());
                    oneTable.addRow(row);
                }
                tables.addDetailTable(oneTable);
            }


             if(null!=info.getData1()){
                 //表示明细表2
                 DetailTable twoTable = new DetailTable();
                 Map<String, Object>[] data1 = info.getData1();
                 twoTable.setId("2");
                 // 行
                 for (int i = 0; i <data1.length; i++) {
                     Row row = new Row();
                     Cell czlx=null;
                     for (String key:data1[i].keySet() ) {
                         if(key.equals("lx")){
                             Integer selectionBox = CommonDao.getSelectionBox(key, String.valueOf(data1[i].get(key)), null);
                             data1[i].put(key,selectionBox);
                         }
                         //过滤掉
                         if(key.equals("id")||key.equals("mainid")){
                             continue;
                         }
                         String value=String.valueOf(data1[i].get(key));
                         if(key.equals("bmyszcjjfl")||key.equals("dyzfjjfl")){
                             value=value.substring(value.indexOf("[")+1,value.indexOf("]"));
                         }
                         czlx = getCell(key, value);
                         //操作类型
                         row.addCell(czlx);
                     }
                     twoTable.addRow(row);
                     LOGGER.info("row2-->"+row.enumerateCell());
                 }
                 tables.addDetailTable(twoTable);
             }
            Map<String,Integer> yslyInfo = getYslyInfo();
            if(null!=info.getData2()){
                //表示明细表3
                DetailTable threeTable = new DetailTable();
                Map<String, Object>[] data2 = info.getData2();
                threeTable.setId("3");
                // 行
                for (int i = 0; i <data2.length; i++) {
                    Row row = new Row();
                    Cell czlx=null;
                    for (String key:data2[i].keySet() ) {
                        if(key.equals("lx")){
                            Integer selectionBox = CommonDao.getSelectionBox(key, String.valueOf(data2[i].get(key)), null);
                            data2[i].put(key,selectionBox);
                        }
                        //过滤掉
                        if(key.equals("id")||key.equals("mainid")){
                            continue;
                        }
                        // 预算来源存储序号
                        if(key.equals("ysly")){
                            Object value = data2[i].get(key);
                            boolean b = yslyInfo.containsKey(value);
                            if(b){
                                Integer integer = yslyInfo.get(value);
                                data2[i].put(key,integer);
                            }
                        }
                        czlx = getCell(key,String.valueOf(data2[i].get(key)));
                        //操作类型
                        row.addCell(czlx);
                    }
                    LOGGER.info("row3-->"+row.enumerateCell());
                    threeTable.addRow(row);
                }
                tables.addDetailTable(threeTable);
            }
            if(null!=info.getData3()){
                //表示明细表4
                DetailTable foreTable = new DetailTable();
                Map<String, Object>[] data3 = info.getData3();
                foreTable.setId("4");
                // 行
                for (int i = 0; i <data3.length; i++) {
                    Row row = new Row();
                    Cell czlx=null;
                    for (String key:data3[i].keySet() ) {
                        if(key.equals("lx")){
                            Integer selectionBox = CommonDao.getSelectionBox(key, String.valueOf(data3[i].get(key)), null);
                            data3[i].put(key,selectionBox);
                        }
                        //过滤掉
                        if(key.equals("id")||key.equals("mainid")){
                            continue;
                        }
                        // 预算来源存储序号
                        if(key.equals("ysly")){
                            Object value = data3[i].get(key);
                            boolean b = yslyInfo.containsKey(value);
                            if(b){
                                Integer integer = yslyInfo.get(value);
                                data3[i].put(key,integer);
                            }
                        }
                        czlx = getCell(key, String.valueOf(data3[i].get(key)));
                        //操作类型
                        row.addCell(czlx);
                    }
                    LOGGER.info("row4-->"+row.enumerateCell());
                    foreTable.addRow(row);
                }
                tables.addDetailTable(foreTable);
            }

            requestinfo.setDetailTableInfo(tables);
            RequestService requestService  = new RequestService();
            String requestid = "";
            try {
                requestid = requestService.createRequest(requestinfo);
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("流程创建成功="+requestid);
            return requestid;
        }


        public Property getPro(String field,String value) {
            Property property = new Property();
            property.setName(field);
            property.setValue(value);
            return property;
        }
        public Cell getCell(String field,String value) {
            Cell cel = new Cell();
            cel.setName(field);
            cel.setValue(value);
            return cel;
        }

    public static void main(String[] args) {
        String str="[30101]基本工资";
        System.out.println(str.substring(str.indexOf("[")+1,str.indexOf("]")));
        Map<String,Integer> map=new HashMap<>();
        map.put("中央财政拨款",1);
        boolean flag = map.containsKey("中央财政拨款");
        System.out.println(flag);

    }
}
